//
//  MAWorkDetailVC.m
//  meiang_edu
//
//  Created by 陈义蒙 on 2018/9/29.
//  Copyright © 2018年 com.meiang.meiang_edu. All rights reserved.
//

#import "MAWorkDetailVC.h"
#import "MAWorkDetailCell.h"
#import "MAOneImgCell.h"
#import "MAHomeModel.h"

@interface MAWorkDetailVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *topArr;
@property (nonatomic, strong) NSArray *btomArr;
@property (nonatomic, strong) UILabel *titL;
@property (nonatomic, strong) UILabel *sendL;

/** models */
@property(nonatomic,strong) NSMutableArray *models;
@property (nonatomic, copy) NSString *reciever_key;
@property (nonatomic,copy) NSString * sender_key;
@property (nonatomic,copy) NSString * keyid1;

@end

@implementation MAWorkDetailVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.topArr =[NSArray array];
    self.btomArr = [NSArray array];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = [NSString stringWithFormat:@"%@详情", @"作业"];
    [self.view addSubview:self.tableView];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make)
    {
        make.edges.equalTo(self.view);
    }];
    [self ma_fetAllData];
}

#pragma mark - datasource && delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section ==0)
    {
        return self.topArr.count;
    }
    else
    {
        return self.models.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==0)
    {
        MAWorkDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"xxeexxdee" forIndexPath:indexPath];
        cell.dicInfo = self.topArr[indexPath.row];
        cell.reciever_key = self.reciever_key;
        cell.sender_key = self.sender_key;
        cell.keyid = self.keyid1;
        return cell;
    }
    else
    {
        MAOneImgCell *oneCell = [MAOneImgCell cellWithTableView:tableView];
        MAHomeModel *model = self.models[indexPath.row];
        oneCell.model = model;
        return oneCell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        return UITableViewAutomaticDimension;
    }
    else
    {
        NSInteger row = indexPath.row;
        MAHomeModel *model = self.models[row];
        return model.cellHeight;
    }
}

/// 数据
- (void)ma_fetAllData
{
    MAUserInfo *userinfo = [MAUserInfoTool userinfo];
    [MBManager showLoading];
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request)
    {
        request.requestSerializerType = ZBJSONRequestSerializer;
        request.URLString = [NSString stringWithFormat:@"%@%@", WorkDetail_url, self.keyid];
        request.methodType = ZBMethodTypeGET;
        [request setValue:userinfo.token forHeaderField:@"token"];
        request.apiType = ZBRequestTypeRefresh;
        DLog(@"%@", request.URLString);
    } success:^(id responseObject, apiType type)
    {
        [MBManager hideAlert];
        // 解析数据
        NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        DLog(@"%@",dataDict);
        NSInteger code = [dataDict[@"msgcode"] integerValue];
        if (code == 1)
        {
            // 请求成功
            [MBManager hideAlert];
            NSDictionary *dict = dataDict[@"value"];
            self.topArr = dict[@"zylst"];
            self.models = [MAHomeModel mj_objectArrayWithKeyValuesArray:dict[@"upload"]];
            self.titL.text = dict[@"message_title"];
            self.sendL.text = [NSString stringWithFormat:@"发送人:%@",dict[@"sender_name"]];
            self.reciever_key = dict[@"reciever_key"];
            self.sender_key = dict[@"sender_key"];
            self.keyid1 = dict[@"keyid"];
            [self.tableView reloadData];
        }
        else
        {
            [MBManager showBriefAlert:dataDict[@"message"]];
        }
    } failure:^(NSError *error)
    {
        DLog(@"%@",error);
    } finished:^(id responseObject, apiType type, NSError *error, BOOL isCache) {
        if (isCache==YES)
        {
            DLog(@"使用了缓存");
        }
    }];
}
#pragma mark - getter
- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.estimatedRowHeight = 150;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[MAWorkDetailCell class] forCellReuseIdentifier:@"xxeexxdee"];
        _tableView.separatorColor = [UIColor whiteColor];
        
        UIView *head =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
        _tableView.tableHeaderView = head;
        UILabel *titl = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH-30, 30)];
        [head addSubview:titl];
        head.backgroundColor =[UIColor whiteColor];
        
        UILabel *sendl =[[UILabel alloc]initWithFrame:CGRectMake(titl.x, titl.bottom, titl.width, titl.height)];
        [head addSubview:sendl];
        sendl.font = [UIFont systemFontOfSize:14];
        self.titL = titl;
        self.sendL = sendl;
    }
    return _tableView;
}


#pragma mark - getter & setter
- (NSMutableArray *)models
{
    if (_models == nil)
    {
        _models = [NSMutableArray array];
    }
    return _models;
}
@end
